package com.dinglian.ots.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.dinglian.ots.dao.model.Store;
import com.dinglian.ots.exception.GlobalException;
import com.dinglian.ots.result.CodeMsg;
import com.dinglian.ots.result.Result;
import com.dinglian.ots.service.AdminService;
import com.dinglian.ots.vo.brand.BrandRankVo;
import com.dinglian.ots.vo.buyer.ExaminationBuyerVo;
import com.dinglian.ots.vo.product.ProductRankVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.xmlbeans.impl.xb.ltgfmt.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author wangbo
 */
@Slf4j
@RestController
@Api(value = "/admin", tags = "管理员模块")
@RequestMapping("/admin/")
@CrossOrigin
public class AdminController {

    @Autowired
    private AdminService adminService;

    @RequiresAuthentication
    @GetMapping("getBuyerExaminationList")
    public String getBuyerExaminationList(@RequestParam(value = "name", required = false) String name,
                                          @RequestParam(value = "phone", required = false) String phone,
                                          @RequestParam(value = "status", required = false) Integer status,
                                          @RequestParam(value = "startTime", required = false) String startTime,
                                          @RequestParam(value = "endTime", required = false) String endTime,
                                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageInfo<ExaminationBuyerVo> examinationBuyerList = adminService.getBuyerExaminationList(name, phone, status, startTime, endTime, pageNum, pageSize);
        JSONObject result = new JSONObject();
        result.put("rows", examinationBuyerList.getList());
        result.put("total", examinationBuyerList.getTotal());

        return result.toJSONString();
    }

    @RequiresAuthentication
    @PostMapping("examineBuyer")
    public Result examineBuyer(Integer buyerId, Integer status) {
        try {
            adminService.examineBuyer(buyerId, status);
        } catch (Exception e) {
            return Result.error(CodeMsg.SERVER_ERROR);
        }

        return Result.successNoData();
    }

    @RequiresAuthentication
    @GetMapping("getBuyerInfo/{id}")
    public Result getBuyerInfo(@PathVariable("id") Integer buyerId) {
        Store store = new Store();
        try {
            store = adminService.getBuyerInfo(buyerId);
        } catch (Exception e) {
            return Result.error(CodeMsg.SERVER_ERROR);
        }

        return Result.success(store);
    }

    @RequiresAuthentication
    @PostMapping("batchExamineBuyer")
    public Result batchExamineBuyer(@RequestParam("ids[]") Integer[] ids, @RequestParam("status") Integer status) {
        try {
            adminService.batchExamineBuyer(ids, status);
        } catch (Exception e) {
            throw new GlobalException(CodeMsg.SERVER_ERROR);
        }
        return Result.successNoData();
    }


    @RequiresAuthentication
    @GetMapping("getBrandRankList")
    public String getBrandRankList(@RequestParam(value = "name", required = false) String name,
                                   @RequestParam(value = "phone", required = false) String phone,
                                   @RequestParam(value = "isRecommend", required = false) Integer isRecommend,
                                   @RequestParam(value = "startTime", required = false) String startTime,
                                   @RequestParam(value = "endTime", required = false) String endTime,
                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageInfo<BrandRankVo> brandRankVos = adminService.getBrandRankList(name, phone, isRecommend, startTime, endTime, pageNum, pageSize);
        JSONObject result = new JSONObject();
        result.put("rows", brandRankVos.getList());
        result.put("total", brandRankVos.getTotal());

        return result.toJSONString();
    }

    @RequiresAuthentication
    @PostMapping("recommendBrand")
    public Result recommendBrand(Integer id, Boolean isRecommend) {
        try {
            adminService.recommendBrand(id, isRecommend);
        } catch (Exception e) {
            return Result.error(CodeMsg.SERVER_ERROR);
        }

        return Result.successNoData();
    }

    @RequiresAuthentication
    @PostMapping("editBrandSort")
    public Result editBrandSort(Integer id, Integer sort) {
        try {
            adminService.editBrandSort(id, sort);
        } catch (Exception e) {
            return Result.error(CodeMsg.SERVER_ERROR);
        }

        return Result.successNoData();
    }


    @RequiresAuthentication
    @PostMapping("batchRecommend")
    public Result batchRecommend(@RequestParam("ids[]") Integer[] ids, @RequestParam("isRecommend") Boolean isRecommend) {
        try {
            adminService.batchRecommend(ids, isRecommend);
        } catch (Exception e) {
            throw new GlobalException(CodeMsg.SERVER_ERROR);
        }
        return Result.successNoData();
    }

    @RequiresAuthentication
    @PostMapping("batchShowBrand")
    public Result batchShowBrand(@RequestParam("ids[]") Integer[] ids, @RequestParam("isShow") Boolean isShow) {
        try {
            adminService.batchShowBrand(ids, isShow);
        } catch (Exception e) {
            throw new GlobalException(CodeMsg.SERVER_ERROR);
        }
        return Result.successNoData();
    }

    @RequiresAuthentication
    @GetMapping("getProductRankList")
    public String getProductRankList(@RequestParam(value = "brandName", required = false) String brandName,
                                   @RequestParam(value = "title", required = false) String title,
                                     @RequestParam(value = "styleName", required = false) String styleName,
                                   @RequestParam(value = "isRecommend", required = false) Integer isRecommend,
                                   @RequestParam(value = "startTime", required = false) String startTime,
                                   @RequestParam(value = "endTime", required = false) String endTime,
                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageInfo<ProductRankVo> productRankVos = adminService.getProductRankList(brandName, title,styleName, isRecommend, startTime, endTime, pageNum, pageSize);
        JSONObject result = new JSONObject();
        result.put("rows", productRankVos.getList());
        result.put("total", productRankVos.getTotal());

        return result.toJSONString();
    }

    @RequiresAuthentication
    @PostMapping("recommendProduct")
    public Result recommendProduct(Integer id, Boolean isRecommend) {
        try {
            adminService.recommendProduct(id, isRecommend);
        } catch (Exception e) {
            return Result.error(CodeMsg.SERVER_ERROR);
        }

        return Result.successNoData();
    }

    @RequiresAuthentication
    @PostMapping("editProductSort")
    public Result editProductSort(Integer id, Integer sort) {
        try {
            adminService.editProductSort(id, sort);
        } catch (Exception e) {
            return Result.error(CodeMsg.SERVER_ERROR);
        }

        return Result.successNoData();
    }

    @RequiresAuthentication
    @PostMapping("batchRecommendProduct")
    public Result batchRecommendProduct(@RequestParam("ids[]") Integer[] ids, @RequestParam("isRecommend") Boolean isRecommend) {
        try {
            adminService.batchRecommendProduct(ids, isRecommend);
        } catch (Exception e) {
            throw new GlobalException(CodeMsg.SERVER_ERROR);
        }
        return Result.successNoData();
    }

    @RequiresAuthentication
    @GetMapping("getTopBrandRankList")
    public String getTopBrandRankList(@RequestParam(value = "name", required = false) String name,
                                   @RequestParam(value = "phone", required = false) String phone,
                                   @RequestParam(value = "isRecommend", required = false) Integer isRecommend,
                                   @RequestParam(value = "startTime", required = false) String startTime,
                                   @RequestParam(value = "endTime", required = false) String endTime,
                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageInfo<BrandRankVo> brandRankVos = adminService.getTopBrandRankList(name, phone, isRecommend, startTime, endTime, pageNum, pageSize);
        JSONObject result = new JSONObject();
        result.put("rows", brandRankVos.getList());
        result.put("total", brandRankVos.getTotal());

        return result.toJSONString();
    }

    @RequiresAuthentication
    @PostMapping("editTopBrandSort")
    public Result editTopBrandSort(Integer id, Integer sort) {
        try {
            adminService.editTopBrandSort(id, sort);
        } catch (Exception e) {
            return Result.error(CodeMsg.SERVER_ERROR);
        }

        return Result.successNoData();
    }
}
